package com.example.a2209_mianshi.ui.mall

import androidx.databinding.ViewDataBinding
import com.bigkoo.convenientbanner.ConvenientBanner
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.example.a2209_mianshi.R
import com.example.a2209_mianshi.databinding.ItemMallHomeBannerBinding
import com.example.a2209_mianshi.databinding.ItemMallHomeGoodsBinding
import com.example.a2209_mianshi.ui.mall.model.MallBanner
import com.example.a2209_mianshi.ui.mall.model.MallHomeRowItem
import com.example.a2209_mianshi.ui.mall.model.MallRowTypes
import com.example.a2209_mianshi.widget.MallBannerHolder

class MallHomeMultiAdapter(data:ArrayList<MallHomeRowItem>):BaseMultiItemQuickAdapter<MallHomeRowItem,BaseDataBindingHolder<ViewDataBinding>>(data),LoadMoreModule {
    private var hasInitBanner = false
    private var bannerIsTurning = false
    init {
        addItemType(MallRowTypes.BANNER, R.layout.item_mall_home_banner)
        addItemType(MallRowTypes.GOODS, R.layout.item_mall_home_goods)
    }
    override fun convert(holder: BaseDataBindingHolder<ViewDataBinding>, item: MallHomeRowItem) {
        when(item.type){
            MallRowTypes.BANNER -> {
                val binding = holder.dataBinding as ItemMallHomeBannerBinding
                convertBanner(binding, item)
            }
            MallRowTypes.GOODS -> {
                val binding = holder.dataBinding as ItemMallHomeGoodsBinding
                convertGoods(binding, item)
            }
        }
    }
    private fun convertGoods(binding: ItemMallHomeGoodsBinding, item: MallHomeRowItem) {
        item.goods?.let {
            binding.item = it.mallGoodsInfoPageResponseList[0]
//            binding.commonPrice.item = it
        }
//        // 查看商品详情
//        binding.root.setOnClickListener {
//            GoodsDetailActivity.jump(item.goods?.id ?: 0, false)
//        }
    }
//    private fun convertRecommend(binding: ItemMallHomeRecommendBinding, item: MallHomeRowItem) {
//        val goodsList = item.goodsList ?: arrayListOf()
//        if (goodsDetailRecommendHelper == null) {
//            goodsDetailRecommendHelper = GoodsDetailRecommendHelper(binding.rvMallRecommend)
//        }
//        goodsDetailRecommendHelper?.display(goodsList)
//    }
    private fun convertBanner(binding: ItemMallHomeBannerBinding, item: MallHomeRowItem) {
        val banners = item.banners
        val bannerCount = banners?.size ?: 0
        if (hasInitBanner) {// 已初始化过Banner
            binding.mallBanner.apply {
                setPages({
                    MallBannerHolder(onBookingClick = {
//                        onBookingClick?.invoke(it)
                    })
                }, banners)

                if (bannerCount > 1) {
                    setPageIndicator(intArrayOf(R.drawable.icon_banner_indicator_0, R.drawable.icon_banner_indicator_1))
                } else {
                    setPageIndicator(intArrayOf(0, 0))
                }
                setPointViewVisible(bannerCount > 1)
                isCanLoop = bannerCount > 1
                isManualPageable = bannerCount >= 1
                if (bannerCount <= 1) {
                    bannerIsTurning = false
                    stopTurning()
                } else if (!bannerIsTurning) {
                    bannerIsTurning = true
                    binding.mallBanner.startTurning(3_000)
                }
                notifyDataSetChanged()
            }
        } else {// 未初始化过Banner
            hasInitBanner = true
            (binding.mallBanner as ConvenientBanner<MallBanner>).apply {
                if (bannerCount > 1) {
                    setPageIndicator(intArrayOf(R.drawable.icon_banner_indicator_0, R.drawable.icon_banner_indicator_1))
                } else {
                    setPageIndicator(intArrayOf(0, 0))
                }
                setPointViewVisible(bannerCount > 1)
                isCanLoop = bannerCount > 1
                setPages({
                    MallBannerHolder(onBookingClick = {
//                        onBookingClick?.invoke(it)
                    })
                }, banners)
                isManualPageable = bannerCount >= 1
            }
            if (bannerCount > 1) {
                bannerIsTurning = true
                binding.mallBanner.startTurning(3_000)
            }
        }
    }
}